package com.mpyy.config;

/**
 * @PackageName:com.mpyy.config
 * @ClassName:SpringmvcConfig
 * @Date:2022-02-23:14:25
 * @Author: Tang Neng
 */

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

/**1、标识为该类为springmvc配置bean*/
@Configuration
/**
 *  ①、基于包进行扫描
 *   <context:component-scan base-package="com.springmvc.handler,com.springmvc.interceptor"/>
 *  */
@ComponentScan(basePackages = {"com.mpyy.handler"})
/**
 *  ④ 启用基于mvc的注解驱动
 *  <mvc:annotation-driven/>
 *  */
@EnableWebMvc
public class SpringmvcConfig implements WebMvcConfigurer{
    public SpringmvcConfig(){
        System.out.println("调用了SpringmvcConfig构造方法");
    }
    @Bean
    public ViewResolver internalResourceViewResolver(){
        InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver();
        internalResourceViewResolver.setPrefix("/");
        internalResourceViewResolver.setSuffix(".jsp");
        return  internalResourceViewResolver;
    }
    /**
     * ③ 处理静态资源的servlet
     * <mvc:default-servlet-handler/>
     * @param configurer
     */
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    /**
     * ⑤ 拦截器
     * <mvc:interceptors>
     <mvc:interceptor>
     <mvc:mapping path="/trans*"/>
     <ref bean="bankTransactionInterceptor"/>
     </mvc:interceptor>
     </mvc:interceptors>
     * @param registry
     */
    /*@Override
    public void addInterceptors(InterceptorRegistry registry) {
        BankTransactionInterceptor bankTransactionInterceptor = new BankTransactionInterceptor();
        registry.addInterceptor(bankTransactionInterceptor).addPathPatterns(new String[]{"/trans*"});
    }*/
}
